package xyz.bernkastel.goods_in_great_demand.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import xyz.bernkastel.goods_in_great_demand.common.CustomException;
import xyz.bernkastel.goods_in_great_demand.common.R;
import xyz.bernkastel.goods_in_great_demand.dto.SetmealDto;
import xyz.bernkastel.goods_in_great_demand.entity.Category;
import xyz.bernkastel.goods_in_great_demand.entity.Setmeal;
import xyz.bernkastel.goods_in_great_demand.entity.SetmealGoods;
import xyz.bernkastel.goods_in_great_demand.service.CategoryService;
import xyz.bernkastel.goods_in_great_demand.service.SetmealGoodsService;
import xyz.bernkastel.goods_in_great_demand.service.SetmealService;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealGoodsService setmealGoodsService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithGoods(setmealDto);
        return R.success("新增套餐成功！");
    }

    /**
     * 分页
     *
     * @param page     第几页
     * @param pageSize 每页的显示数量
     * @param name     指定的套餐名
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //分页构造器
        Page pageInfo = new Page(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        lambdaQueryWrapper.like(!StringUtils.isEmpty(name), Setmeal::getName, name);//对应sql语句的like查询
        //添加排序条件
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);//按照更新时间排序
        //执行查询
        setmealService.page(pageInfo, lambdaQueryWrapper);//不需要返回值，函数内部自动封装

        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> recordsDto = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //拷贝数据
            BeanUtils.copyProperties(item, setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查找分类对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;


        }).collect(Collectors.toList());
        //设置信息
        dtoPage.setRecords(recordsDto);

        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     *
     * @param ids 套餐id列表
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeWithGoods(ids);
        return R.success("套餐数据删除成功！");

    }

    /**
     * 启售套餐
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable("status") Integer status, @RequestParam List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids != null, Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, status);
        int count = setmealService.count(queryWrapper);
        if (count > 0) {
            throw new CustomException("套餐状态不一致，或所有套餐已为该状态！");
        }
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(ids != null, Setmeal::getId, ids);
        List<Setmeal> list = setmealService.list(queryWrapper1);//得到所有需要更改状态的套餐
        for (Setmeal setmeal : list) {
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);

        }


        return R.success("操作完成！");

    }

    @GetMapping("{id}")
    public R<SetmealDto> gain(@PathVariable Long id) {
        Setmeal setmeal = setmealService.getById(id);//得到套餐信息
        SetmealDto setmealDto = new SetmealDto();//创建回显数据
        BeanUtils.copyProperties(setmeal, setmealDto);//复制信息
        LambdaQueryWrapper<SetmealGoods> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealGoods::getSetmealId, setmeal.getId());
        List<SetmealGoods> list = setmealGoodsService.list(queryWrapper);//得到所有商品信息
        setmealDto.setSetmealGoods(list);

        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> gain(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithGoods(setmealDto);

        return R.success("修改成功");
    }

    /**
     * 根据条件查询套餐数据
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

}
